package leetcode.N101_N200;

/**
 * @author xiwai [hww282306@alibaba-inc.com]
 * @version 1.0
 * @date 2020/10/19 4:38 下午
 */
public class N151 {

    public static void main(String[] args) {
        String s = "the sky is blue";
        System.out.println(reverseWords(s));
    }

    public static String reverseWords(String s) {
        StringBuilder sb = new StringBuilder();
        char blank = ' ';
        StringBuilder sbb = new StringBuilder();
        for (int i = s.length() - 1; i >= 0; i--) {
            char c = s.charAt(i);
            if (c == blank) {
                if (sbb.length() > 0) {
                    sb.append(' ');
                    sb.append(sbb.reverse());
                    sbb.setLength(0);
                }
            } else {
                sbb.append(c);
            }
        }
        if (sbb.length() > 0) {
            sb.append(' ');
            sb.append(sbb.reverse());
        }

        return sb.substring(1).toString();
    }


}
